package com.stone.framework.task.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.stone.framework.message.PageReqMessage;
import com.stone.framework.message.PageResMessage;
import com.stone.framework.message.ReqMessage;
import com.stone.framework.message.ResMessage;
import com.stone.framework.message.task.ScheduleConfigPageReqBody;
import com.stone.framework.message.task.ScheduleConfigPageResBody;
import com.stone.framework.message.task.ScheduleConfigReqBody;
import com.stone.framework.task.service.ScheduleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "定时任务")
@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation(value = "分页查询", httpMethod = "POST")
    @PostMapping(value = "/getPage")
    PageResMessage<ScheduleConfigPageResBody>
        getPage(@Valid @RequestBody PageReqMessage<ScheduleConfigPageReqBody> reqMessage) {
        IPage<ScheduleConfigPageResBody> page = scheduleService.queryPage(reqMessage.getBody(), reqMessage.buildPage());
        return PageResMessage.success(page);
    }

    @ApiOperation(value = "新增", httpMethod = "POST")
    @PostMapping(value = "/add")
    ResMessage<Void> add(@Valid @RequestBody ReqMessage<ScheduleConfigReqBody> reqMessage) {
        scheduleService.save(reqMessage.getBody());
        return ResMessage.success();
    }

    @ApiOperation(value = "修改", httpMethod = "POST")
    @PostMapping(value = "/update")
    ResMessage<Void> update(@Valid @RequestBody ReqMessage<ScheduleConfigReqBody> reqMessage) {
        scheduleService.modify(reqMessage.getBody());
        return ResMessage.success();
    }

    @ApiOperation(value = "刪除", httpMethod = "POST")
    @PostMapping(value = "/delete")
    ResMessage<Void> delete(@Valid @RequestBody ReqMessage<Long> reqMessage) {
        scheduleService.delete(reqMessage.getBody());
        return ResMessage.success();
    }

    @ApiOperation(value = "执行", httpMethod = "POST")
    @PostMapping(value = "/run")
    ResMessage<Void> run(@Valid @RequestBody ReqMessage<Long> reqMessage) {
        scheduleService.run(reqMessage.getBody());
        return ResMessage.success();
    }

    @ApiOperation(value = "暂停", httpMethod = "POST")
    @PostMapping(value = "/pause")
    ResMessage<Void> pause(@Valid @RequestBody ReqMessage<Long> reqMessage) {
        scheduleService.pause(reqMessage.getBody());
        return ResMessage.success();
    }

    @ApiOperation(value = "恢复", httpMethod = "POST")
    @PostMapping(value = "/resume")
    ResMessage<Void> resume(@Valid @RequestBody ReqMessage<Long> reqMessage) {
        scheduleService.resume(reqMessage.getBody());
        return ResMessage.success();
    }

}
